/*
 * @Author: ZhanXie
 * @Date: 2019-12-30 11:48:40
 * @LastEditors  : ZhanXie
 * @LastEditTime : 2019-12-31 10:37:10
 * @Description: 
 * @FilePath: /node-demo/blog-koa/routes/user.js
 */
const router = require('koa-router')()
const { SuccessModel, ErrorModel } = require('../model/resModel')
const { login } = require('../controller/user')

router.prefix('/api/user')

router.post('/login', async function (ctx, next) {
  const { username, password } = ctx.request.body
  const data = await login(username, password)
  if (data.username) {
    // 设置 session
    ctx.session.username = data.username
    ctx.session.realname = data.realname
    ctx.body = new SuccessModel(data)
    return
  }
  ctx.body = new ErrorModel('登录失败')
})

router.get('/login-test', async function (ctx, next) {
  if (ctx.session.username) {
    res.json(new SuccessModel('已成功'))
  } else {
    res.json(new ErrorModel('失败'))
  }
})

module.exports = router
